<template>
  <div class="classificationOuter" :style="{ background: `url('${bgImg}') no-repeat`, backgroundSize: 'cover' }">
    <HeaderPage :headerNavindex="4" />

    <div class="classification">
      <div class="classification_item" v-if="categoryData.length !==0" v-for="item in categoryData"
        @click="getclassificationItem(item.categoryName)">

        <div class="classification_item_inner">
          <p>{{item.categoryName}}</p>
        </div>

      </div>
      <div class="category_null" v-else>
        <span>数据为空</span>
      </div>
    </div>

    <div class="articleList" v-if="categoryList.length !==0">
      <div class="articleListItem" v-for="item in categoryList" :key="item._id" @click="toArticleDetile(item._id)">
        <img :src="item.articleCover" alt="背景">
        <p>{{item.title}}</p>
        <p>{{item.introduction}}</p>
        <div class="bottom">
          <span>{{item.category}}</span>
          <span>{{item.tag}}</span>
        </div>
      </div>
    </div>

    <FooterPage :fixed="true" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

import HeaderPage from '../../../components/HeaderPage.vue'
import FooterPage from '../../../components/FooterPage.vue';

import { findImgApi } from '../../../service/apiModule/bgImgApi'
import { getCategoryApi } from '../../../service/apiModule/categoryApi'
import { searchArticleOneApi } from '../../../service/apiModule/articleApi'

const router = useRouter()

onMounted(() => {
  getBgImg()
  getCategory()
})

// 背景
let bgImg = ref('http://nevergiveupt.top/category.jpg')

// 获取背景
const getBgImg = async () => {
  const { data: res } = await findImgApi()
  if (res.status.code !== 200) {
    bgImg.value = ''
  } else {
    res.data.forEach((item: any) => {
      if (item.bgTitle === '分类背景') {
        bgImg.value = item.imgSrc
      }
    })
  }
}

// 分类数据
const categoryData = ref([
  {
    categoryName: "",
    categoryNum: 0,
    _id: "",
  },
]);

// 获取分类
const getCategory = async () => {
  const { data: res } = await getCategoryApi({ categoryName: "" })
  if (res.status.code !== 200) {
    categoryData.value = []
  } else {
    categoryData.value = res.data.list
  }
}

const categoryList = ref([])

// 获取一种分类
const getclassificationItem = async (data: String) => {
  const { data: res } = await searchArticleOneApi({
    type: 'classification',
    title: data
  })
  categoryList.value = res.data
  console.log(categoryList.value)
}

// 去文章详情
const toArticleDetile = (id: any) => {
  router.push({
    path: '/article/detail',
    query: { id }
  })
}


</script>

<style lang="scss" scoped>
.classificationOuter {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  color: white;
  position: relative;
  top: 0;
  padding-top: 80px;

  .classification {
    width: 80%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    margin-top: 40px;

    .classification_item {
      width: 23%;
      color: black;
      text-align: center;
      cursor: pointer;
      border-radius: 10px;
      overflow: hidden;

      .classification_item_inner {
        width: 100%;
        height: 100%;
        padding: 20px 0 10px 0;
        background-color: orange;
        font-size: 12px;

        p {
          width: 100%;
          padding-bottom: 16px;
          font-size: 20px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;

          &::after {
            content: "";
            display: block;
            height: 2px;
            width: 20%;
            background-color: #fff;
            margin-top: 10px;
          }
        }

        span {
          font-size: 16px;
        }
      }
    }

    .category_null {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 40px;
      padding: 40px 0;
      color: #fff;
      font-weight: 300;
    }
  }

  .articleList {
    width: 80%;
    min-height: 160px;
    border: 1px solid;
    margin: 0 auto;
    margin-top: 20px;
    background-color: rgba(215, 251, 255, 0.9);
    padding: 10px;
    border-radius: 10px;

    .articleListItem {
      background-color: #fff;
      height: 160px;
      width: 140px;
      color: #000;
      cursor: pointer;

      img {
        width: 100%;
        height: 80px;
      }

      &>p {
        font-size: 14px;

        &:nth-child(2) {
          font-weight: bold;
          text-align: center;
          margin: 6px 0;
        }

        &:nth-child(3) {
          margin: 6px 0;
          font-size: 13px;
          padding-left: 10px;
        }
      }

      .bottom {
        font-size: 12px;
        width: 50%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-left: 10px;
      }
    }
  }
}
</style>